package com.uphone.magicleafteacher.pro.activity.startstudent;

import android.arch.lifecycle.LifecycleOwner;

import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.base.mvp.BasePAV;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.http.FailureConsumer;
import com.uphone.magicleafteacher.http.RxSchedulers;
import com.uphone.magicleafteacher.http.SuccessfulConsumer;
import com.uphone.magicleafteacher.util.GsonUtils;
import com.uphone.magicleafteacher.vo.StartStudentBean;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

public class StartStudentPresenter extends BasePAV<StartStudentContract.View> implements StartStudentContract.Presenter {

    @Inject
    public StartStudentPresenter() {
    }

    public int page;


    @Override
    public void postStart(int page) {
        this.page = page;
        Map<String, Object> map1 = new HashMap<>();
        map1.put("page", page + "");
        map1.put("limit", "10");
        MyApp.apiService(ApiService.class)
                .startList(map1)
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    mView.showLoading();
                })
                .doFinally(() -> {
                    mView.closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) mView)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        StartStudentBean startStudentBean = GsonUtils.getGson().fromJson(jsonObject, StartStudentBean.class);
                        mView.updateView(startStudentBean);
                    }
                }, new FailureConsumer());


    }
}
